# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# All available actions: https://docs.fastlane.tools/actions
# can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available:
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.28.3"

default_platform :ios

platform :ios do
  before_all do
    # desc "update cocoapods"
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
    #cocoapods
    
  end
  
  #fastlane ipa_param domain:http://1.2.3.4 port:8333
  desc "参数测试"
    lane :ipa_param do |options|
    domain = options[:domain]
    port = options[:port]
    puts("doamin is #{domain}")
    puts("port is #{port}")
    puts("url is #{domain}:#{port}")
    end
    
  desc "打测试包"
  lane :ipa_adhoc do
    #更新版本号、git信息
    sh("./updateBuild.sh MKLab-adhoc;")
        
    filename = get_info_plist_value(
        path:"fastlane/BuildConfig.plist",
        key:"FILENAME"
    )
    
    puts("ipa file:#{filename}.ipa")
        
    gym(
        scheme:"MKLab-adhoc",
        export_method:"development",
        output_directory:"./ipa_test",
        archive_path:"./ipa_test/#{filename}.xcarchive",
        output_name:"#{filename}.ipa"
    )

    #记录目标文件名称
    sh("/usr/libexec/PlistBuddy -c \"Set :IPA ipa_test/#{filename}.ipa\" ./BuildConfig.plist || /usr/libexec/PlistBuddy -c \"Add :IPA string ipa_test/#{filename}.ipa\" ./BuildConfig.plist")
    
    #删除测试包的符号表
    sh("rm -rf \"../ipa_test/#{filename}.app.dSYM.zip\"")
    #删除测试包的打包中间文件
    sh("rm -rf \"../ipa_test/#{filename}.xcarchive\"")
  end
  
  desc "打生产包"
  lane :ipa_prod do
    #更新版本号、git信息
    sh("./updateBuild.sh MKLab-prod;")
    
    
    filename = get_info_plist_value(
        path:"fastlane/BuildConfig.plist",
        key:"FILENAME"
    )
    
    puts("ipa file:#{filename}.ipa")
        
    gym(
        scheme:"MKLab-prod",
        export_method:"app-store",
        output_directory:"./ipa_prod",
        archive_path:"./ipa_prod/#{filename}.xcarchive",
        output_name:"#{filename}.ipa",
        include_symbols:true
    )
    
    #记录目标文件名称
    sh("/usr/libexec/PlistBuddy -c \"Set :IPA ipa_prod/#{filename}.ipa\" ./BuildConfig.plist || /usr/libexec/PlistBuddy -c \"Add :IPA string ipa_prod/#{filename}.ipa\" ./BuildConfig.plist")
  end
  
  # You can define as many lanes as you want

  after_all do |lane|
    # This block is called, only if the executed lane was successful

    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end

  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end


# More information about multiple platforms in fastlane: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used
# No personal data is recorded. Learn more at https://github.com/fastlane/enhancer
